package com.demo.core.dao;

import java.util.List;

import org.hibernate.Query;
import org.hibernate.Session;
import org.springframework.stereotype.Repository;

import com.demo.base.model.InvitationCode;
import com.demo.base.util.CommonUtil;

@Repository
public class InvitationCodeDao extends BaseDao{

	/**
	 * 验证邀请码是否可用
	 * @param code
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public boolean validateInvitationCode(String code){
		
		Session session = this.getSession();
		Query query = session.createQuery("from InvitationCode where code = :code and enable = '0'");
		query.setString("code", code);
		List<InvitationCode> list = query.list();
		if(!CommonUtil.isNull(list)){
			return true;
		}
		
		return false;
	}
	
	/**
	 * 根据邀请码 获取邀请码实体
	 * @param code
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public InvitationCode getInvitationCodeBycode(String code){
		
		Session session = this.getSession();
		Query query = session.createQuery("from InvitationCode where code = :code");
		query.setString("code", code);
		List<InvitationCode> list = query.list();
		
		if(!CommonUtil.isNull(list)){
			
			return list.get(0);
		}
		
		return null;
	}
}
